#!/usr/bin/env bash

# @author Winston Koh
# @author Ilayaperumal Gopinathan
# @since 1.0

##############################################################################
##
##  install-redis script based on the platform
##
##############################################################################

# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '/.*' > /dev/null; then
        PRG="$link"
    else
        PRG=`dirname "$PRG"`"/$link"
    fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/.." >&-
APP_HOME="`pwd -P`"
cd "$SAVED" >&-

REDIS_VERSION=redis-2.8.3
REDIS_ZIP_NAME="$REDIS_VERSION.tar.gz"
REDIS_HOME=$APP_HOME
REDIS_ZIP_PATH="$REDIS_HOME/dist/$REDIS_ZIP_NAME"
REDIS_BIN="$REDIS_HOME/bin"

if [[ ! -f $REDIS_ZIP_PATH ]]; then
  echo "Corrupted redis zip. $REDIS_ZIP_PATH not found."
  exit
fi

# find out platform info
unameinfo=$(uname -a)
platforminfo=""
platformunsupported="false"

if [[ $unameinfo == Darwin* ]]; then
  platforminfo="mac64"
  echo "$platforminfo platform detected."
  if [[ $platformunsupported == "false" ]]; then
    cd $REDIS_HOME && tar xzf $REDIS_ZIP_PATH
    if [[ ! -d $REDIS_VERSION ]]; then
      echo "Corrupted redis zip. $REDIS_VERSION not found."
      exit
    fi
    cd $REDIS_VERSION
    make
  fi
elif [[ $unameinfo == Linux*_64* ]]; then
  if [[ -f /etc/redhat-release ]]; then
    platforminfo="rh64"
    echo "$platforminfo platform detected."
    elif [[ -f /etc/centos-release ]]; then
      platforminfo="cent64"
      echo "$platforminfo platform detected."
    elif [[ -f /etc/debian_version ]]; then
      platforminfo="ubuntu64"
      echo "$platforminfo platform detected."
    elif [[ -f /etc/slackware-version ]]; then
      platforminfo="slackware64"
      echo "$platforminfo platform detected."
    else
      echo "Unsupported Linux 64 bit platform."
      platformunsupported="true"
    fi
    if [[ $platformunsupported == "false" ]]; then
      cd $REDIS_HOME && tar xzf $REDIS_ZIP_PATH
      if [[ ! -d $REDIS_VERSION ]]; then
        echo "Corrupted redis zip. $REDIS_VERSION not found."
        exit
      fi
      cd $REDIS_VERSION
      make
    fi
elif [[ $unameinfo == Linux* ]]; then
  if [[ -f /etc/redhat-release ]]; then
    platforminfo="rh32"
    echo "$platforminfo platform detected."
  elif [[ -f /etc/centos-release ]]; then
    platforminfo="cent32"
    echo "$platforminfo platform detected."
  elif [[ -f /etc/debian_version ]]; then
    platforminfo="ubuntu32"
    echo "$platforminfo platform detected."
  elif [[ -f /etc/slackware-version ]]; then
    platforminfo="slackware32"
    echo "$platforminfo platform detected."
  else
    echo "Unsupported Linux 32 bit platform."
    platformunsupported="true"
  fi
  if [[ $platformunsupported == "false" ]]; then
    cd $REDIS_HOME && tar xzf $REDIS_ZIP_PATH
    if [[ ! -d $REDIS_VERSION ]]; then
      echo "Corrupted redis zip. $REDIS_VERSION not found."
      exit
    fi
    cd $REDIS_VERSION
    #make 32bit
    #https://github.com/antirez/redis/issues/736 
    make CFLAGS="-march=i686"
  fi
else
  echo "Unsupported platform. Curently only Apple OS X and Linux systems (RedHat, CentOS, Ubuntu, Slackware) are supported."
  platformunsupported="true"
fi
if [[ $platformunsupported == "false" ]]; then
  cp src/redis-server $REDIS_BIN 
  cp src/redis-sentinel $REDIS_BIN 
  cp src/redis-benchmark $REDIS_BIN 
  cp src/redis-cli $REDIS_BIN 
  cp src/redis-check-dump $REDIS_BIN 
  echo "installation script completed."
  echo "Selecting a non-default memory allocator when building Redis is done by setting 
the MALLOC environment variable. Please refer to $REDIS_HOME/$REDIS_VERSION/README."
  echo "To start redis server, please run: $REDIS_BIN/redis-server"
fi

